<?php
class PiVoucherHeader extends AppModel {
    var $useDbConfig  = 'mssql';
    var $useTable     = 'piVoucherHeaders';
    var $primaryKey   = 'piVoucherHeaderID';
    var $displayField = 'InvoiceNo';
    var $name         = 'PiVoucherHeader';
    var $hasOne = array(
        'PiVoucherDetail' => array(
            'className'  => 'PiVoucherDetail',
            'foreignKey' => 'piVoucherHeaderID',
            'fields'     => 'piVoucherHeaderID, PiVoucherHeader.TransactionDate, PiVoucherHeader.CreateDate, documentno, Debit',
            'dependent'  => true
        )
    );

    public function coupon_list ($code) {
        $data = $this->find('all', array(
            'conditions' => array('PiVoucherDetail.MemberCode' => $code),
            'order'      => array('PiVoucherHeader.piVoucherHeaderID')
        ));

        $rec  = array();

        if (count($data) == 0) {
            return 0;
        }

        foreach ($data as $rs) {
            $resp[] = array(
                'id'        => $rs['PiVoucherHeader']['piVoucherHeaderID'],
                'ref'       => $rs['PiVoucherHeader']['InvoiceNo'],
                'amount'    => number_format($rs['PiVoucherDetail']['Debit'],2),
                'datetime'  => date_format($rs['PiVoucherHeader']['CreateDate'], 'd/m/Y h:ia'),
                'sortTime'  => date_format($rs['PiVoucherHeader']['CreateDate'], 'Y-m-d H:i:s'),
                'effective' => 'Yes',
                'type'      => 'BV',
                'details'   => __('This coupon is created because you have ordered enough BV in the previous month. To activate this coupon, you need to pay the associated order.',true)
            );
        }

        return $resp;
    } // coupon_list
} // PiVoucherHeader
?>